package raku7.server;

import java.util.LinkedList;

import raku.packet.Packet;
public class Room {
	boolean shooting_1;
	boolean shooting_2;
	int shoot = 0;
	Shoot shoot_1;
	Shoot shoot_2;
	ServerProgram frame;
	private LinkedList<Connection>userList;
	public Room(Connection master, ServerProgram sp){
		frame = sp;
		userList = new LinkedList<Connection>();
		userList.add(master);
		shooting_1 = false;
		shooting_2 = false;
	}
	public String getUserIp(int i){
		return userList.get(i).getIP();
	}
	public boolean joinUser(Connection c) throws Exception{
		if(userList.size() <2){
			userList.add(c);
			sendToEnemy(c, new Packet(Packet.JOIN_ROOM));
			return true;
		}else{
			return false;
		}
	}
	public void leaveRoom(Connection c) throws Exception{
		sendToEnemy(c, new Packet(Packet.QUIT_ROOM));
		c.setInRoom(false);
		userList.remove(c);
		if(userList.size() < 1){
			frame.removeRoom(this);
		}
	}
	public int userNum(){
		return userList.size();
	}
	
	public void sendToEnemy(Connection attacker, Packet p) throws Exception{
		
		frame.logMessage(attacker.getIP()+" sent "+p.getType()+" type packet\n");
		
		if(p.getType() == Packet.JOIN_ROOM){
			userList.get(0).sendPacket(p);
			userList.get(1).sendPacket(p);
			return;
		}
		
		if(p.getType() == Packet.RESTART){
			shoot = 0;
			userList.get(0).sendPacket(new Packet(Packet.FAKE));
			userList.get(1).sendPacket(new Packet(Packet.FAKE));
			return;
		}

		if(attacker == userList.get(0)){
			if(p.getType() == Packet.SHOOT){
				if(shooting_2){
					shoot_1 = new Shoot(attacker, 1000 , 0);
					shoot_1.start();
				}else{
					shooting_1 = true;
					shoot_1 = new Shoot(attacker, 2000 , 0);
					shoot_1.start();
				}
			}
			if(p.getType() == Packet.WIN){
				if(shoot == 0){
					shoot = 1;
					userList.get(1).sendPacket(new Packet(Packet.LOSE));
					userList.get(0).sendPacket(p);
				}
			}else{
				userList.get(1).sendPacket(p);
			}
		}else{
			if(p.getType() == Packet.SHOOT){
				if(shooting_1){
					shoot_2 = new Shoot(attacker, 1000 , 1);
					shoot_2.start();
				}else{
					shooting_2 = true;
					shoot_2 = new Shoot(attacker, 2000, 1);
					shoot_2.start();
				}
			}
			if(p.getType() == Packet.WIN){
				if(shoot == 0){
					shoot = 1;
					userList.get(0).sendPacket(new Packet(Packet.LOSE));
					userList.get(1).sendPacket(p);
				}
			}else{
				userList.get(0).sendPacket(p);
			}
		}	
	}

	private class Shoot extends Thread{
		int time;
		int who;
		Connection attacker;
		Shoot(Connection attacker, int time , int who){
			this.time = time;
			this.who = who;
			this.attacker = attacker;
		}
		public void run(){
			try {
				Thread.sleep(time);
			} catch (InterruptedException e) {}


			if(who == 1){
				shooting_1 = false;
			}else{
				shooting_2 = false;
			}
			try {
				sendToEnemy(attacker, new Packet(Packet.WIN));
			} catch (Exception e) {}
		}
	}
}
